import { getViewerData } from '@/api/common'

import { useUserStore } from '@/store/user'

import { DCM_MODULE_FLAG_MAP } from '@/configs/codeMaps'
import IBaseObj from '#/index'

/**
 * 配置window.open(参数)
 */
const windowOpen = (options: { [key: string]: string }) => {
  const defaultOptions = {
    url: '',
    name: 'reportViewer',
    specs:
      'top=0,left=0,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no,channelmode=yes,fullscreen=yes',
  }

  const mergeOptions = {
    ...defaultOptions,
    ...options,
  }

  const { url, name, specs } = mergeOptions

  const instance = window.open(url, name, specs)

  if (instance) {
    const larg = instance.screen.availWidth
    const altez = instance.screen.availHeight

    instance.moveTo(0, 0)
    instance.resizeTo(larg, altez)
  }
}

// 默认为只查看
const openViewer = async (data: IBaseObj, moduleFlags: number = DCM_MODULE_FLAG_MAP.chakan) => {
  console.log('da', data)
  const { studyUid, vid } = data
  const params = {
    moduleFlags,
    studyUid,
    vid,
    operId: useUserStore().uid,
  }
  const res: any = await getViewerData(params)
  if (res) {
    windowOpen({ url: `${res}?flag=${moduleFlags}` })
  }
}

export { DCM_MODULE_FLAG_MAP, openViewer, windowOpen }
